const { wxLogin } = require('../../utils/http/api')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    show: 0,
    banner: [
      {
        picUrl: 'https://imut.oss-cn-hangzhou.aliyuncs.com/LiJiaming/map.png'
      },
      {
        picUrl: 'https://imut.oss-cn-hangzhou.aliyuncs.com/LiJiaming/phone.png'
      },
      {
        picUrl: 'https://imut.oss-cn-hangzhou.aliyuncs.com/LiJiaming/show.png'
      }
    ],
  },
  /**
   * 修改show值
   */
  changeShow() {
    if (this.data.show < 3) {
      let num = this.data.show
      this.setData({
        show: num + 1
      })
    }
  },
  /**
   * 页面跳转，跳转到主页
   */
  getStart() {
    let _this = this
    let imutUser = wx.getStorageSync('imutUser')
    let userInfo = wx.getStorageSync('userInfo')
    if (!userInfo || !imutUser) {
      _this.Login() //重新登录
    } else {
      wx.switchTab({
        url: '/pages/index/index',
      })
    }
  },
  // 判断缓存是否存在用户信息
  judgeImutuser() {
    let imutUser = wx.getStorageSync('imutUser')
    let userInfo = wx.getStorageSync('userInfo')
    if (imutUser && userInfo) {
      wx.reLaunch({
        url: '../index/index',
      })
    }
  },
  onShow() {
    let _this = this
    async function show() {
      await _this.judgeImutuser()
    }
    show()
  },
  /**
   * 登录获取openid、session_key
   */
  Login() {
    wx.showLoading({
      title: '登录中...',
    })
    wx.login({
      success(res) {
        if (res.code) {
          const data = {
            "clientId": "wx7027bbc25959f98a",
            "rememberMe": true,
            "grantType": "xcx",
            "appid":"wxe193560cee1bb01a",
            "secret":"54c66d195cd275ac8b89f3ce623d4d66",
            "xcxCode": res.code
          }
          console.log("login",data)
          wxLogin(data).then((res) => {
            wx.setStorageSync('userInfo', res.data)
            wx.hideLoading()
            wx.reLaunch({
              url: '../index/index',
            })
          })
        } else {
          wx.showToast({
            title: '请稍后再试',
            icon: "error"
          })
        }
      },
      fail: function (err) {
        wx.showToast({
          title: '进入 err' + err,
        })
      }
    })
  },
  /**
   * 换取openid
   * @param {*} event 
   */
  /**
   * 轮播current值监视
   */
  changeCurrent(event) {
    this.setData({
      show: event.detail.current
    })
  },

  /**
   * 监听回调
   */
  handleGetUserInfo(e) {
    wx.setStorageSync('imutUser', e.detail.userInfo)
    this.getStart()
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

})